package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.ums.entity.UmsCollect;
import org.jeecg.modules.job.ums.service.IUmsCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *收藏模块管理
 */
@RestController
@RequestMapping("/api/collect")
@Api(tags="移动端浏览记录管理模块")
@Slf4j
public class CollectController{

    @Autowired
    private IUmsCollectService collectService;

    /**
     * 查询收藏列表
     */
    @RequestMapping(value = "/getCollectList",method = RequestMethod.GET)
    @ApiOperation(value = "查询收藏列表", notes = "查询收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public Result<Object> getCollectList(UmsCollect param,
                                         @RequestParam(name="page", defaultValue="1") Integer page,
                                         @RequestParam(name="limit", defaultValue="10") Integer limit
    ) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<UmsCollect> pageInfo= collectService.findPageBySpec(new Page<>(page,limit),param);
        return Result.OK(pageInfo);
    }

    /**
     * 查询收藏详情
     */
    @RequestMapping(value = "/getCollectDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询收藏详情", notes = "查询收藏详情")
    public Result<Object> getCollectDetail(String id) {
        UmsCollect result=collectService.getById(id);
        return Result.OK(result);
    }

    /**
     * 添加/更新收藏
     */
    @RequestMapping(value = "/addOrUpdateCollect",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新收藏", notes = "添加/更新收藏")
    public Result<Object> addOrUpdateCollect(@RequestBody UmsCollect param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (user.getMemberRole()==1){//企业端浏览记录
                param.setComMemberId(user.getId());
            }else{
                param.setMemberId(user.getId());
            }
            collectService.addOrUpdate(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除收藏
     */
    @RequestMapping(value = "/deleteCollect",method = RequestMethod.POST)
    @ApiOperation(value = "删除收藏", notes = "删除收藏")
    public Result<Object> deleteCollect(@RequestBody UmsCollect param) {
        try {
            collectService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



